package com.koi.clubservice.Utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.koi.clubservice.domain.po.CourseSchedule;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
// 处理 Excel 读取数据的监听器
public class ClassScheduleDataListener extends AnalysisEventListener<CourseSchedule> {
    private List<CourseSchedule> dataList = new ArrayList<>();

    // 每读取一行数据时调用
    @Override
    public void invoke(CourseSchedule data, AnalysisContext context) {


        dataList.add(data);
    }

    // 读取完成后调用
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("数据读取完成，共读取 " + dataList.size() + " 条记录。");
    }

    // 处理异常
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        if (exception instanceof com.alibaba.excel.exception.ExcelDataConvertException) {
            com.alibaba.excel.exception.ExcelDataConvertException convertException = (com.alibaba.excel.exception.ExcelDataConvertException) exception;
            System.err.println("第 " + convertException.getRowIndex() + " 行，第 " + convertException.getColumnIndex() + " 列数据转换异常，数据为: " + convertException.getCellData());
        } else {
            System.err.println("解析失败: " + exception.getMessage());
        }
    }

    public List<CourseSchedule> getDataList() {
        return dataList;
    }
}